package gunPowderMod.entities;

import java.util.Iterator;
import java.util.Random;

import net.minecraft.src.Block;
import net.minecraft.src.ChunkPosition;
import net.minecraft.src.Entity;
import net.minecraft.src.EntityTNTPrimed;
import net.minecraft.src.Explosion;
import net.minecraft.src.MathHelper;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.World;

public class CustomExplosion extends Explosion {

	private float destructionRatio;
	private World worldObj;
	protected Random explosionRNG = new Random();

	public CustomExplosion(World par1World, Entity par2Entity, double par3, double par5, double par7, float par9, float destructionRatio) {
		super(par1World, par2Entity, par3, par5, par7, par9);
		this.destructionRatio = destructionRatio;
		this.worldObj = par1World;
	}

	@Override
	/**
	 * Does the second part of the explosion (sound, particles, drop spawn)
	 */
	public void doExplosionB(boolean par1) {
		this.worldObj.playSoundEffect(this.explosionX, this.explosionY, this.explosionZ, "random.explode", 4.0F, (1.0F + (this.worldObj.rand.nextFloat() - this.worldObj.rand.nextFloat()) * 0.2F) * 0.7F);

		if (this.explosionSize >= 2.0F && this.field_82755_b) {
			this.worldObj.spawnParticle("hugeexplosion", this.explosionX, this.explosionY, this.explosionZ, 1.0D, 0.0D, 0.0D);
		} else {
			this.worldObj.spawnParticle("largeexplode", this.explosionX, this.explosionY, this.explosionZ, 1.0D, 0.0D, 0.0D);
		}

		Iterator var2;
		ChunkPosition var3;
		int var4;
		int var5;
		int var6;
		int var7;
		if (this.field_82755_b) {
			var2 = this.field_77281_g.iterator();

			while (var2.hasNext()) {
				var3 = (ChunkPosition) var2.next();
				var4 = var3.x;
				var5 = var3.y;
				var6 = var3.z;
				var7 = this.worldObj.getBlockId(var4, var5, var6);
				
				if (par1) {
					double var8 = (double) ((float) var4 + this.worldObj.rand.nextFloat());
					double var10 = (double) ((float) var5 + this.worldObj.rand.nextFloat());
					double var12 = (double) ((float) var6 + this.worldObj.rand.nextFloat());
					double var14 = var8 - this.explosionX;
					double var16 = var10 - this.explosionY;
					double var18 = var12 - this.explosionZ;
					double var20 = (double) MathHelper.sqrt_double(var14 * var14 + var16 * var16 + var18 * var18);
					var14 /= var20;
					var16 /= var20;
					var18 /= var20;
					double var22 = 0.5D / (var20 / (double) this.explosionSize + 0.1D);
					var22 *= (double) (this.worldObj.rand.nextFloat() * this.worldObj.rand.nextFloat() + 0.3F);
					var14 *= var22;
					var16 *= var22;
					var18 *= var22;
					this.worldObj.spawnParticle("explode", (var8 + this.explosionX * 1.0D) / 2.0D, (var10 + this.explosionY * 1.0D) / 2.0D, (var12 + this.explosionZ * 1.0D) / 2.0D, var14, var16, var18);
					this.worldObj.spawnParticle("smoke", var8, var10, var12, var14, var16, var18);
				}

				if (var7 > 0) {
					Block.blocksList[var7].dropBlockAsItemWithChance(this.worldObj, var4, var5, var6, this.worldObj.getBlockMetadata(var4, var5, var6), this.destructionRatio, 0);

					if (this.worldObj.setBlockAndMetadataWithUpdate(var4, var5, var6, 0, 0, !this.worldObj.isRemote)) {
						this.worldObj.notifyBlocksOfNeighborChange(var4, var5, var6, 0);
					}

					Block.blocksList[var7].onBlockDestroyedByExplosion(this.worldObj, var4, var5, var6);
				}
				this.worldObj.markBlockNeedsUpdate(var4, var5, var6);
			}
		}

		if (this.isFlaming) {
			var2 = this.field_77281_g.iterator();

			while (var2.hasNext()) {
				var3 = (ChunkPosition) var2.next();
				var4 = var3.x;
				var5 = var3.y;
				var6 = var3.z;
				var7 = this.worldObj.getBlockId(var4, var5, var6);
				int var24 = this.worldObj.getBlockId(var4, var5 - 1, var6);

				if (var7 == 0 && Block.opaqueCubeLookup[var24] && this.explosionRNG.nextInt(3) == 0) {
					this.worldObj.setBlockWithNotify(var4, var5, var6, Block.fire.blockID);
				}
			}
		}
	}
}
